package hadoop;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


import javax.servlet.http.HttpServlet;
import java.io.IOException;

import java.net.URI;

/**
 * @auther 郭天凌
 * @date 2021/12/4
 */
public class hadooputil  {
    //加载hdfs的路径

         String url = "hdfs://192.168.232.11:9000/";
        public String hadoopupload(String srcPath,String destPath) throws IOException {

            String url_new=url;
            //加载hdfs的配置
            Configuration conf = new Configuration();
            FileSystem fileSystem = FileSystem.get(URI.create(url_new), conf);
            Path srcloc = new Path(srcPath);
            Path destloc = new Path(destPath);
            fileSystem.copyFromLocalFile(srcloc,destloc);
             return  "上传成功!" ;
            }

            public String hadoopRead(String filepath) throws IOException

            {
                String reaUrl = url+ filepath;
                Configuration conf = new Configuration();

               // 建立hdfs连接
                FileSystem fileSystem = FileSystem.get(URI.create(reaUrl),conf);
                FSDataInputStream fsDataInputStream = fileSystem.open(new Path(reaUrl));
                IOUtils.copyBytes(fsDataInputStream, System.out, 4096,false);

                System.out.println( fsDataInputStream);
                fsDataInputStream.close();

                return "读取成功!";

            }
    public String hadoopDowload(String srcPath,String destPath) throws IOException
     {
         //String reaUrl = url+ destPath;
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(url),conf);
        // 建立hdfs连接
        // 本地的 文件
        Path srcLoc =new Path(srcPath);
        // hdfs 的路径
        Path destLoc = new Path(destPath);
         System.out.println(srcLoc);
         System.out.println(destLoc);
        fileSystem.copyToLocalFile(false,srcLoc,destLoc,true);

        // fileSystem.copyToLocalFile(false, new Path("/test18/导航.txt"), new Path("D:/hello1.txt"), true);
        return "下载成功";
    }


}
